package SecondeStage.IO.Reader_Writer;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class FileReader_ {
    public static void main(String[] args) {
        String filePath ="D:\\Java\\JavaProject\\hello.txt";
        FileReader fileReader =null;
        int data=0;
        int readLen =0;
        char[] buf =new char[8];
        try {
            fileReader = new FileReader(filePath);
            // 循环读取，使用read
//            while ((data=fileReader.read())!=-1){
//                System.out.print((char)data);
//            }
            // 一次读取多个字符到字符数组
            while ((readLen = fileReader.read(buf))!=-1){
                System.out.print(new String(buf,0,readLen));
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            if(fileReader!=null){
                try {
                    // FileReader使用后，必须要关闭(close)或刷新(flush)，否则写入不到内存
                    fileReader.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}
